<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

class Cate extends Model
{
    protected $table = 'category';
    protected $primaryKey = 'cate_id';

    protected $fillable = [
        'cate_pid', 'cate_name', 'cate_keywords', 'cate_description', 'cate_order', 'cate_title'
    ];

    protected $hidden = [
    ];
    public $timestamps = false;
    //排序并给分类名称添加缩进
    public function tree(){
       $category =  $this->orderBy('cate_order','asc')->get();
       return  $this->getTree($category,0);
    }
    //给分类名称添加缩进
    public function getTree($cate,$pid = 0){
        $arr = array();
        foreach($cate as $k=>$v){
            //        遍历一级分类
            if($v->cate_pid == $pid){
                $cate[$k]['_cate_name'] = $cate[$k]['cate_name'];
                $arr[] = $cate[$k];
                foreach ($cate as $m=>$n){
                    if($v->cate_id == $n->cate_pid){
                        $cate[$m]['_cate_name']=" |-|-". $cate[$m]['cate_name'];
                        $arr[]=$cate[$m];
                    }
                }
            }
        }
        return $arr;

    }
}
